﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.TeamFoundation.Framework.Client;
using System.Collections.ObjectModel;
using System.Net;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Framework.Common;
using Microsoft.TeamFoundation.VersionControl.Client;
using System.Collections;

namespace DatabaseSynchronizer {

    public class TFSService {


        private readonly Uri _tfsUri;
        private readonly NetworkCredential _networkCredentials;
        private readonly TfsTeamProjectCollection _tfsServer;
        private ReadOnlyCollection<CatalogNode> _projects;

        private static Guid[] _qryAll = new[] {
        CatalogResourceTypes.AnalysisDatabase,
        CatalogResourceTypes.ApplicationDatabase,
        CatalogResourceTypes.DataCollector,
        CatalogResourceTypes.GenericLink,
        CatalogResourceTypes.InfrastructureRoot,
        CatalogResourceTypes.Machine,
        CatalogResourceTypes.OrganizationalRoot,
        CatalogResourceTypes.ProcessGuidanceSite,
        CatalogResourceTypes.ProjectCollection,
        CatalogResourceTypes.ProjectCollectionDatabase,
        CatalogResourceTypes.ProjectPortal,
        CatalogResourceTypes.ReportingConfiguration,
        CatalogResourceTypes.ReportingFolder,
        CatalogResourceTypes.ReportingServer,
        CatalogResourceTypes.ResourceFolder,
        CatalogResourceTypes.SharePointSiteCreationLocation,
        CatalogResourceTypes.SharePointWebApplication,
        CatalogResourceTypes.SqlAnalysisInstance,
        CatalogResourceTypes.SqlDatabaseInstance,
        CatalogResourceTypes.SqlReportingInstance,
        CatalogResourceTypes.TeamFoundationServerInstance,
        CatalogResourceTypes.TeamFoundationWebApplication,
        CatalogResourceTypes.TeamProject,
        CatalogResourceTypes.TeamSystemWebAccess,
        CatalogResourceTypes.TestController,
        CatalogResourceTypes.TestEnvironment,
        CatalogResourceTypes.WarehouseDatabase
        };

        public TFSService(Uri tfsUri, NetworkCredential networkCredentials) {

            _tfsUri = tfsUri;
            _networkCredentials = networkCredentials;

            if (_networkCredentials == null) {
                _tfsServer = new TfsTeamProjectCollection(_tfsUri);
            }
            else {
                _tfsServer = new TfsTeamProjectCollection(_tfsUri, CredentialFactory.CreateCredentials(_networkCredentials));
            }

        }


        public ReadOnlyCollection<CatalogNode> GetProjectCollection(out string errorMsg) {

            ReadOnlyCollection<CatalogNode> result = null;
            errorMsg = string.Empty;

            try {
            
                result = _tfsServer.ConfigurationServer.CatalogNode
                            .QueryChildren(new[] { CatalogResourceTypes.ProjectCollection },
                                    false, CatalogQueryOptions.None);
            }
            catch (Exception ex) {
                errorMsg = ex.Message;
            }

            _projects = result;

            return result;
        }


        public ReadOnlyCollection<CatalogNode> GetProjectFolders(CatalogNode tfsCollection, out string errorMsg) {

            ReadOnlyCollection<CatalogNode> result = null;
            errorMsg = string.Empty;

            try {
                result = tfsCollection.QueryChildren(_qryAll,
                                                    false, CatalogQueryOptions.None);
            }
            catch (Exception ex) {
                errorMsg = ex.Message;
            }

            return result;
        }

        public ReadOnlyCollection<CatalogNode> GetProjectFolders(string path, out string errorMsg) {

            ReadOnlyCollection<CatalogNode> result = null;
            errorMsg = string.Empty;

            try {

                VersionControlServer vcs = _tfsServer.GetService(typeof(VersionControlServer)) as VersionControlServer;

                var workspaces = vcs.QueryWorkspaces(string.Empty, _tfsServer.ConfigurationServer.ClientCacheDirectoryForInstance, Environment.MachineName);
                ItemSet items = vcs.GetItems("$/EHSHealthPortal/Solution/");


            }
            catch (Exception ex) {
                errorMsg = ex.Message;
            }

            return result;
        }

        public ReadOnlyCollection<CatalogNode> GetProjectFolders(Guid collectionId, Guid projectNodeId, out string errorMsg) {

            ReadOnlyCollection<CatalogNode> result = null;
            errorMsg = string.Empty;

            try {
                List<KeyValuePair<string, string>> filter = new List<KeyValuePair<string, string>>();
                filter.Add(new KeyValuePair<string, string>("Identifier", projectNodeId.ToString()));

                result = _projects.Single(p => p.Resource.Identifier == collectionId)
                                .QueryChildren(_qryAll,
                                                    filter, false, CatalogQueryOptions.None);
            }
            catch (Exception ex) {
                errorMsg = ex.Message;
            }

            return result;
        }



    }
}
